package com.dudu.common.util

import android.text.Spannable
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.CharacterStyle
import android.text.style.ForegroundColorSpan
import androidx.annotation.ColorRes


/**
 * <pre>
 *     author : dzc
 *     time   : 2024/11/07
 *     desc   :
 * </pre>
 */
object TextUtil {

    /**
     * 文本高亮
     */
    fun getHighLightText(
        content: String, @ColorRes colorId: Int,
        start: Int, end: Int
    ): CharSequence {
        if (TextUtils.isEmpty(content)) {
            return ""
        }
        val spannable = SpannableString(content)
        spannable.setSpan(
            ForegroundColorSpan(getResColor(colorId)),
            if (start >= 0) start else 0,
            if (end <= content.length) end else content.length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        return spannable
    }

}